﻿using System;
using System.Security.Cryptography;

namespace MyWatermark
{

    public static class FileNameGenerator
    {
        /// <summary>
        /// 生成带时间标记的安全随机文件名
        /// </summary>
        public static string GenerateTimestampedName(string extension = "")
        {
            string timePart = DateTime.UtcNow.ToString("yyyyMMddTHHmmssfff");
            string randomPart = Convert.ToHexString(RandomNumberGenerator.GetBytes(4));
            return string.IsNullOrEmpty(extension)
                ? $"{timePart}_{randomPart}"
                : $"{timePart}_{randomPart}{EnsureDotExtension(extension)}";
        }

        /// <summary>
        /// 生成短随机文件名 (8字符)
        /// </summary>
        public static string GenerateShortName()
        {
            return Convert.ToHexString(RandomNumberGenerator.GetBytes(4)).ToLowerInvariant();
        }

        private static string EnsureDotExtension(string ext)
        {
            return ext.StartsWith('.') ? ext : $".{ext}";
        }
    }

}
